package com.north.light.libpermission;

import android.app.Activity;
import android.content.Context;

import androidx.fragment.app.Fragment;

import com.north.light.libpermission.api.PermissionCallback;
import com.north.light.libpermission.constant.PermissionCode;
import com.north.light.libpermission.constant.PermissionReflex;
import com.north.light.libpermission.impl.PermissionFactory;
import com.north.light.libpermission.intercept.PermissionIntercept;
import com.north.light.libpermission.intercept.PermissionInterceptImpl;
import com.north.light.libpermission.intercept.dialog.InterceptDialogCallback;

import java.io.Serializable;
import java.util.List;

/**
 * FileName: PermissionUtils
 * Author: lzt
 * Date: 2022/8/17 14:30
 * <p>
 * 调用该方法，必须配合Permissions中的权限列表使用
 */
public class PermissionUtils implements Serializable {
    private Context mContext;
    private PermissionIntercept mIntercept;
    //权限类型
    private static PermissionType mPermissionType = PermissionType.TYPE_PERMISSION_XX;
    //请求权限组
    private String[] mRequestGroup;
    //请求回调
    private PermissionCallback mCallback;
    //请求标识--key
    private int mReqKey = 0;
    //是否拒绝后进入设置页面
    private boolean mAutoEnterSetting = false;


    public PermissionUtils(Context context) {
        this.mContext = context;
    }

    //内部调用-------------------------------------------------------------------------------------


    /**
     * 权限是否包含检查
     */
    private boolean containPermission(String[] permission) {
        return PermissionReflex.getInstance().containPermission(permission);
    }

    /**
     * 开始请求对象
     */
    private void build() {
        try {
            if (mContext == null) {
                notifyError(PermissionCode.CODE_ERROR_PERMISSION_CONTEXT);
                return;
            }
            if (mRequestGroup == null || mRequestGroup.length == 0) {
                //申请权限为空
                notifyError(PermissionCode.CODE_ERROR_PERMISSION_STR);
                return;
            } else {
                //检查权限
                if (!containPermission(mRequestGroup)) {
                    notifyError(PermissionCode.CODE_NO_SUPPORT);
                    return;
                }
            }
            //拦截器初始化
            initIntercept();
            //开始请求权限
            realRequestPermission();
        } catch (Exception e) {
            notifyError(PermissionCode.CODE_OTHER);
        }
    }

    /**
     * 请求权限
     */
    private void realRequestPermission() throws Exception {
        //拦截器处理------------------------------------
        //是否需要显示弹窗
        PermissionIntercept permissionIntercept = getIntercept();
        boolean needEnsureDialog = permissionIntercept.ensureDialog((Activity) mContext, new InterceptDialogCallback() {
            @Override
            public void confirm() {
                requestPermission();
            }

            @Override
            public void cancel() {

            }
        });
        if (needEnsureDialog) {
            return;
        }
        requestPermission();
    }

    private void requestPermission() {
        PermissionFactory.create(mPermissionType).requestPermission((Activity) mContext, mRequestGroup, mReqKey, mAutoEnterSetting, new PermissionCallback() {
            @Override
            public void result(int type, int status, boolean grantAll) {
                notifySuccess(type, status, grantAll);
            }

            @Override
            public void result(int type, int status, List<String> permissions, boolean grantAll) {
                notifySuccess(type, status, permissions, grantAll);
            }

            @Override
            public void error(int code) {
                notifyError(code);
            }
        });
    }


    //回调事件-------------------------------------------------------------------------------------

    private void notifyError(int code) {
        if (mCallback != null) {
            mCallback.error(code);
        }
    }

    private void notifySuccess(int type, int status, boolean grantAll) {
        if (mCallback != null) {
            mCallback.result(type, status, grantAll);
        }
    }

    private void notifySuccess(int type, int status, List<String> permission, boolean grantAll) {
        if (mCallback != null) {
            mCallback.result(type, status, permission, grantAll);
        }
    }


    //权限请求拦截器初始化---------------------------------------------------------------------------

    private void initIntercept() {
        if (mIntercept == null) {
            mIntercept = initDefaultIntercept();
        }
    }

    public PermissionIntercept getIntercept() {
        initIntercept();
        return mIntercept;
    }

    /**
     * 默认拦截器
     */
    private PermissionIntercept initDefaultIntercept() {
        return new PermissionInterceptImpl();
    }


    //外部调用-------------------------------------------------------------------------------------
    //外部调用-------------------------------------------------------------------------------------
    //外部调用-------------------------------------------------------------------------------------


    /**
     * builder 模式
     */

    //context
    public static PermissionUtils with(Activity activity) {
        return new PermissionUtils(activity);
    }

    public static PermissionUtils with(Fragment fragment) {
        return new PermissionUtils(fragment.requireContext());
    }

    public static PermissionUtils with(android.app.Fragment fragment) {
        return new PermissionUtils(fragment.getActivity());
    }

    //拦截器
    public PermissionUtils intercept(PermissionIntercept intercept) {
        this.mIntercept = intercept;
        return this;
    }

    //权限请求框架类型
    public PermissionUtils type(PermissionType type) {
        this.mPermissionType = type;
        return this;
    }

    //permission
    public PermissionUtils request(String[] permission) {
        this.mRequestGroup = permission;
        return this;
    }


    //key
    public PermissionUtils key(int key) {
        this.mReqKey = key;
        return this;
    }

    //listener
    public PermissionUtils setListener(PermissionCallback callback) {
        this.mCallback = callback;
        return this;
    }

    //拒绝后自动进入设置页面
    public PermissionUtils autoIntoSetting(boolean enter) {
        this.mAutoEnterSetting = enter;
        return this;
    }

    //build
    public PermissionUtils start() {
        build();
        return this;
    }

    /**
     * 判断方法
     */
    public static boolean isGrant(Context context, String[] permissions) {
        return isGrant(context, permissions, mPermissionType);
    }

    public static boolean isGrant(Context context, String[] permissions, PermissionType permissionType) {
        return PermissionFactory.create(permissionType).isGrant(context, permissions);
    }

}
